package base

import (
	"git.mycaigou.com/gfyx/common/rpc_client"
	"git.mycaigou.com/gfyx/micro-gfyx-api/http_server/controller"
	"git.mycaigou.com/gfyx/micro-gfyx-api/infrastructure/common/errcode"
	"git.mycaigou.com/gfyx/micro-gfyx-api/infrastructure/dto/base"
	"git.mycaigou.com/gfyx/micro-gfyx-api/service/base/msg"
	"git.myscrm.cn/golang/stark/v4"
	"github.com/gin-gonic/gin"
)

type MsgController struct {
	controller.BaseController
	msgService msg.MsgServiceIface
}

func NewMsgController(msgService msg.MsgServiceIface) *MsgController {
	return &MsgController{
		msgService: msgService,
	}
}

// RegisterRouter
// 注册路由
func (c *MsgController) RegisterRouter(g *gin.RouterGroup) {
	// 消息列表
	g.POST("/msg/list", c.MsgList)
	g.POST("/msg/set-readed", c.MsgSetReaded)
}


func (c *MsgController) MsgSetReaded(ctx *gin.Context) {
	req := &base.MsgSetReadedParam{}
	if err := ctx.ShouldBind(req); err != nil {
		stark.Logger.Infof(ctx, "MsgSetReaded-error: %+v", err)
		c.Fail(ctx, errcode.COMMON_PARAMS_ERROR, err.Error())
		return
	}

	msgId, err := c.msgService.MsgSetReaded(ctx, req)
	if err != nil {
		stark.Logger.Infof(ctx, "MsgSetReaded-err: %+v, req: %+v", err, req)
		grpcErr := rpc_client.GetGrpcError(err)
		c.Fail(ctx, grpcErr.GetCode(), grpcErr.GetMessage())
		return
	}

	c.Success(ctx, map[string]interface{}{
		"id": msgId,
	})
}

//func (c *MsgController) MsgDelete(ctx *gin.Context) {
//	req := &base.MsgDeleteParams{}
//	if err := ctx.ShouldBind(req); err != nil {
//		stark.Logger.Infof(ctx, "MsgDelete-error: %+v", err)
//		c.Fail(ctx, errcode.COMMON_PARAMS_ERROR, err.Error())
//		return
//	}
//
//	if req.MsgId == 0 {
//		c.Fail(ctx, errcode.COMMON_PARAMS_ERROR, "部门编号为空")
//		return
//	}
//
//	msgId, err := c.msgService.MsgDelete(ctx, req)
//	if err != nil {
//		stark.Logger.Infof(ctx, "MsgDelete-err: %+v, req: %+v", err, req)
//		grpcErr := rpc_client.GetGrpcError(err)
//		c.Fail(ctx, grpcErr.GetCode(), grpcErr.GetMessage())
//		return
//	}
//
//	c.Success(ctx, map[string]interface{}{
//		"id": msgId,
//	})
//}

func (c *MsgController) MsgList(ctx *gin.Context) {
	param := &base.MsgListParam{}
	if err := ctx.ShouldBind(param); err != nil {
		stark.Logger.Infof(ctx, "MsgListParam-error: %+v", err)
		c.Fail(ctx, errcode.COMMON_PARAMS_ERROR, err.Error())
		return
	}

	listData, err := c.msgService.MsgList(ctx, param)
	if err != nil {
		stark.Logger.Infof(ctx, "MsgList-err: %+v", err)
		grpcErr := rpc_client.GetGrpcError(err)
		c.Fail(ctx, grpcErr.GetCode(), grpcErr.GetMessage())
		return
	}

	c.Success(ctx, listData)
}
//
//func (c *MsgController) MsgImport(ctx *gin.Context) {
//	c.Success(ctx, nil)
//}
